/**
 * 
 */
package au.edu.swin.videoshop.ejb.bean;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import au.edu.swin.videoshop.bean.Store;
import au.edu.swin.videoshop.persistence.StaffEntity;
import au.edu.swin.videoshop.persistence.StoreEntity;
import au.edu.swin.videoshop.service.StoreService;

/**
 * @author julie
 *
 */
@Stateless
public class StoreServiceBean implements StoreService{
	
	@PersistenceContext
	private EntityManager em;

	
	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.StoreService#addStore(au.edu.swin.videoshop.bean.Store)
	 */
	public void addStore(Store store) {
		TransferService service = new TransferService();
		StoreEntity storeEntity = service.toStoreEntity(store);
		em.persist(storeEntity);
	}

	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.StoreService#updateStore(au.edu.swin.videoshop.bean.Store)
	 */
	public void updateStore(Store store) {
		TransferService service = new TransferService();
		StoreEntity storeEntity = service.toStoreEntity(store);
		em.merge(storeEntity);
	}

	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.StoreService#markDeleteStore(long)
	 */
	public void markDeleteStore(long id) {
		StoreEntity storeEntity = em.find(StoreEntity.class, id);
		if (storeEntity != null) {
			storeEntity.setDeleted(true);
			em.merge(storeEntity);
		}
	}
	
	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.StoreService#findAllStores()
	 */
	public List<Store> findAllStores() {
		Query query = em.createNamedQuery("findAllStores");
		List entityList = query.getResultList();
		TransferService service = new TransferService();
		return service.toStoreTransferObjectList(entityList);
	}

	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.StoreService#findStoreByStoreId(long)
	 */
	public Store findStoreByStoreId(long id) {
		StoreEntity storeEntity = em.find(StoreEntity.class, id);
		if (storeEntity != null) {
			TransferService service = new TransferService();
			return service.toStoreTransferObject(storeEntity);
		}
		
		return null;
	}
}
